<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="卡券名称" prop="search">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入卡券名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_normal_disable"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading" :data="Lists" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
            <el-table-column label="id" align="center" prop="coupon_id"  width="50"/>
            <el-table-column label="名称" align="center" prop="coupon_title" :show-overflow-tooltip="true" />
            <el-table-column prop="type" align="center" label="类型" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_coupon_type" :value="row.type"/>
                </template>
            </el-table-column>
            <el-table-column label="支付面值" align="center" prop="price" />
            <el-table-column label="到账面值" align="center" prop="give_money" />
            <el-table-column label="有效天数" align="center" prop="valid_days" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="排序" align="center" prop="sort" />

            <el-table-column prop="status" align="center" label="是否使用" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_yes_no" :value="row.status"/>
                </template>
            </el-table-column>
            <el-table-column prop="is_user" align="center" label="客户端显示【是否】" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_yes_no" :value="row.is_user"/>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="created_time"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="{row}">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(row)"
                    >修改</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />

        <!-- 添加或修改部门对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="优惠券名称" prop="coupon_title">
                    <el-input v-model="form.coupon_title" placeholder="请输入优惠券名称" />
                </el-form-item>
                <el-form-item label="优惠劵类型" >
                    <el-radio-group v-model="form.type">
                        <el-radio
                            v-for="dict in dict.type.sys_coupon_type"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="实际面值：">
                    <el-input
                        :controls="false"
                        v-model="form.price"
                        class="content_width"
                    ></el-input>
                </el-form-item>
                <el-form-item label="到账面值：">
                    <el-input-number
                        :min="1"
                        :max="9999999999"
                        v-model="form.give_money"
                        class="content_width"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="时效天数" prop="valid_days">
                    <el-input v-model="form.valid_days" placeholder="请输入时效天数" />
                </el-form-item>

                <el-form-item label="是否使用" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_yes_no"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否显示【用户端】" prop="is_user">
                    <el-radio-group v-model="form.is_user">
                        <el-radio
                            v-for="dict in dict.type.sys_yes_no"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {getWaterCouponList,getWaterCouponDetail,createWaterCoupon,updateWaterCoupon,delWaterCoupon} from "@/api/property/water_coupon";

export default {
    name: "master_read",
    dicts: ['sys_yes_no','sys_coupon_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            loadingRead: true,
            // 显示搜索条件
            showSearch: true,
            isEdit: false,
            // 表格数据
            Lists: [],
            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                coupon_title: [
                    { required: true, message: "卡券名称不能为空", trigger: "blur" }
                ],
                price: [
                    { required: true, message: "实际面值不能为空", trigger: "blur" }
                ],
                valid_days: [
                    { required: true, message: "时效天数不能为空", trigger: "blur" }
                ],
            },
        };
    },
    computed:{

    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getWaterCouponList(this.queryParams);
            that.Lists = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                coupon_id: undefined,
                coupon_title: undefined,
                type: "1",
                price: undefined,
                give_money: undefined,
                valid_days: undefined,
                remark: undefined,
                status: "Y"
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            let that = this;
            this.reset();
            that.enumOptions=[];
            const coupon_id = row.coupon_id || this.ids
            getWaterCouponDetail({coupon_id:coupon_id}).then(response => {
                let data= response.data;
                that.form = data;
                that.form.type = data.type.toString();
                that.open = true;
                that.title = "修改";
            });
        },

        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.coupon_id != undefined) {
                        updateWaterCoupon(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        createWaterCoupon(this.form).then(response => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.confirm('是否确认删除名称为"' + row.item_name + '"的数据项？').then(function() {
                return delCheckItem({coupon_id:row.coupon_id});
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => {});
        }
    }
};
</script>

<style lang="scss" scoped>
.options-editor {
    margin-bottom: 20px;
}
.data-add-container {
    padding: 24px;
    background: #fff;
    border-radius: 8px;
}

.config-section {
    border-left: 1px solid #eee;
    padding-left: 24px;
    min-height: 100px;
}
.input-newtag{
    margin: 10px;
}
.input-new-tag {
    width: 150px;
    margin-top: 10px;
}

.json-editor {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    height: 100px;
}

.range-separator {
    margin: 0 10px;
    color: #999;
}

.form-footer {
    margin-top: 24px;
    text-align: center;
}

.data-preview {
    margin-top: 30px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 4px;
}

</style>
